<?php

/*
 * 用户表
 */

namespace app\models;

use yii\db\ActiveRecord;
use Yii;
use yii\web\UploadedFile;

class Users extends ActiveRecord {

    public $password_confirm;

    /**
     * @return string 返回该AR类关联的数据表名
     */
    public static function tableName() {
        return 'user';
    }

    /**
     * 增加声望
     * @param int $uid
     * @param int $prestige
     */
    public function add_prestige($prestige, $uid = null) {
        if (!$uid) {
            $uid = Yii::$app->user->identity->id;
        }
        $user_info = Users::findOne(['id' => $uid]);
        $user_info->prestige = $user_info->prestige + $prestige;
        $user_info->save();
    }

    /**
     * 减去声望
     * @param int $uid
     * @param int $prestige
     */
    public function minus_prestige($prestige, $uid = null) {
        if (!$uid) {
            $uid = Yii::$app->user->identity->id;
        }
        $user_info = Users::findOne(['id' => $uid]);
        $new_prestige = $user_info->prestige - $prestige;
        if ($new_prestige < 0) {
            $new_prestige = 0;
        }
        $user_info->prestige = $new_prestige;
        $user_info->save();
    }

    /**
     * 在插入之前操作,保存加密过的密码和时间
     * @param type $insert
     * @return type
     */
    public function beforeSave($insert) {
        if ($this->isNewRecord) {
            $this->password = Yii::$app->getSecurity()->generatePasswordHash($this->password);
            $this->rid = '2';
            $this->created_at = time();
            $this->updated_at = time();
        } else {
            $this->updated_at = time();
        }
        return parent::beforeSave($insert);
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => '用户ID',
            'rid' => '角色ID',
            'username' => '用户名',
            'email' => '邮箱',
            'pasword' => '密码',
            'prestige' => '声望',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    public function edit($post, $model) {
        $id = Yii::$app->user->identity->id;
        $user = Users::findOne($id);
        //1、判断是否修改了图片
        $files = UploadedFile::getInstance($model, 'avatar');
        if ($files && !$files->getHasError()) {
            //判断图片格式是否正常
            $file_extend = $files->getExtension();
            if (!in_array($file_extend, array('jpg', 'png', 'gif'))) {
                return false;
            }
            //判断图片大小
            if($files->size > 1024000){
                return FALSE;
            }
            //重新生成名字
            $new_name = $id . '.' . $file_extend;
            $avatar_file = 'images/avatar/' . $new_name;
            $files->saveAs($avatar_file);
            $user->avatar = '/' . $avatar_file;
        }

        //2、判断是否修改了密码
        if ($post['Users']['password']) {
            if ($post['Users']['password'] != $post['Users']['password_confirm']) {
                return false;
            } else {
                $user->password = Yii::$app->getSecurity()->generatePasswordHash($post['Users']['password']);
            }
        }
        //3、保存擅长
        $user->good = $post['Users']['good'];
        $user->save();
        return true;
    }

}
